package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.common.vo.FinancialVo;
import com.gxa.entity.SysFinancial;
import com.gxa.entity.SysProviderProgress;
import com.gxa.mapper.FinancialManagementMapper;
import com.gxa.service.FinancialManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FinancialManagementServiceImpl implements FinancialManagementService {

    @Autowired
    FinancialManagementMapper financialManagementMapper;


    /**
     * 查询所有财务信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R findFinancialList(Integer currentPage, Integer pageSize) {
        R r = new R();
        if(currentPage !=null && pageSize !=null){
        PageHelper.startPage(currentPage,pageSize);
        List financialList = financialManagementMapper.findFinancialList();
        PageInfo<SysProviderProgress> pageInfo = new PageInfo(financialList);
        if(pageInfo != null && financialList.size()>0){
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }else{
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        }else {
            r.fromStatus(Status.BAD_QUERY_PARAM);
        }
        return r;
    }

    @Override
    public R addFinancial(FinancialVo financialVo) {
        R r = new R();
        int i = financialManagementMapper.addFinancial(financialVo);
        if (i>0){
            r.setCode(200);
            r.setMsg("success");
        }else {
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    /**
     * 根据姓名查财务信息
     * @param head
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R findFinancialByHead(String head, Integer currentPage, Integer pageSize) {
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List financialByHead = financialManagementMapper.findFinancialByHead(head);
        PageInfo<SysProviderProgress> pageInfo = new PageInfo(financialByHead);
        if(pageInfo != null && financialByHead.size() > 0){
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }else {
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    /**
     * 修改财务信息
     * @param sysFinancial
     * @return
     */
    @Override
    public R updateFinancial(SysFinancial sysFinancial) {
        R r = new R();
        int i = financialManagementMapper.updateFinancial(sysFinancial);
        if (i > 0) {
            r.setCode(200);
            r.setMsg("success");
        } else {
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    /**
     * 根据id删除财务
     * @param id
     * @return
     */
    @Override
    public R deleteFinancial(Integer id) {
        R r = new R();
        int i = financialManagementMapper.deleteFinancial(id);
        if (i > 0) {
            r.setCode(200);
            r.setMsg("success");
        } else {
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

}
